package com.finger.forest.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.finger.forest.R;

/**
 * @author sy
 * @date 2020/6/1
 * @desc
 */
public class PopupWindowForUploadHead extends PopupWindow {
    private View mView; // PopupWindow 菜单布局
    private Context mContext; // 上下文参数
    private View.OnClickListener mSelectListener; // 相册选取的点击监听器

    public TextView titleTv;

    public PopupWindowForUploadHead(Context context, View.OnClickListener selectListener) {
        super(context);
        this.mContext = context;
        this.mSelectListener = selectListener;
        init();
    }

    /**
     * 设置布局以及点击事件
     */
    @SuppressLint("ClickableViewAccessibility")
    private void init() {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.popu_layout, null);
        Button btn_camera = (Button) mView.findViewById(R.id.cameraBn);
        Button btn_select = (Button) mView.findViewById(R.id.selectBn);
        Button btn_cancel = (Button) mView.findViewById(R.id.cancelBn);

        titleTv = mView.findViewById(R.id.titleTv);

        btn_select.setOnClickListener(mSelectListener);
        btn_camera.setOnClickListener(mSelectListener);
        btn_cancel.setOnClickListener(v -> dismiss());

        // 导入布局
        this.setContentView(mView);
        // 设置动画效果
        this.setAnimationStyle(R.style.popwindow_anim_style);
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        // 设置可触
        this.setFocusable(true);
        ColorDrawable drawable = new ColorDrawable(0x0000000);
        this.setBackgroundDrawable(drawable);
        // 单击弹出窗以外关闭窗口
        mView.setOnTouchListener((v, event) -> {
            int height = mView.findViewById(R.id.ll_pop).getTop();
            int y = (int) event.getY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (y < height) {
                    dismiss();
                }
            }
            return true;
        });
    }
}
